package leetcode_2401_2500;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class LeeCode_2423 {
    public static void main(String[] args) {
        System.out.println(equalFrequency("abbcc"));
    }
    private static boolean equalFrequency(String word) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (map.containsKey(c)){
                map.put(c, map.get(c) + 1);
            }else {
                map.put(c, 1);
            }
        }
        int[] arr = new int[map.size()];
        Set<Character> keys = map.keySet();
        int index = 0;
        for (Character key : keys) {
            arr[index++] = map.get(key);
        }
        Arrays.sort(arr);
        if (arr.length == 1 || arr[arr.length - 1] == 1){
            return true;
        }
        return (arr[0] == arr[arr.length - 2] && arr[arr.length - 1] == arr[0] + 1) ||
                (arr[0] == 1 && arr[1] == arr[arr.length - 1]);
    }
}
